<?php

namespace App\Admin\Controllers;

use App\Models\Forum;
use App\Models\ForumTaxonomy;
use Encore\Admin\Http\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Table;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ForumController extends AdminController
{
    protected $title = "版块";


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function table()
    {
        $table = new Table(new Forum);
        $table->model()->with('taxonomies');

        $table->id('Id');
        $table->name('Name');
        $table->icon('Icon');
        $table->description('Description');
        $table->taxonomies('所属分类')->pluck('name')->label();
        $table->column('created_at','Created at');
        $table->column('updated_at','Updated at');

        $table->filter(function ($filters) {
            $filters->like('name', '名称');
            $filters->where(function ($query) {
                $query->whereHas("taxonomies", function ($query) {
                    $query->where("taxonomy_id", $this->input);
                });
            }, "所属分类", "taxonomy")->radio(ForumTaxonomy::all()->pluck('name', 'id'));
        });

        return $table;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Forum::findOrFail($id));

        $show->id('Id');
        $show->key('Key');
        $show->name('Name');
        $show->icon('Icon');
        $show->description('Description');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Forum);

        $form->text('name', 'Name');
        $form->text('icon', 'Icon');
        $form->textarea('description', 'Description');
        $form->checkbox('taxonomies', '所属分类')->options(ForumTaxonomy::all()->pluck('name', 'id'));

        return $form;
    }
}
